﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhieuMuaHang : Form
    {
        private string _maPhieu;

        public ThemPhieuMuaHang()
        {
            InitializeComponent();
        }

        public ThemPhieuMuaHang(string maPhieuMuaHang)
        {
            InitializeComponent();
            _maPhieu = maPhieuMuaHang;
        }


        private void ThemSuaPhieuMuaHang_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // if edit, show info
            if (_maPhieu != null)
            {
                _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuMuaHang == _maPhieu);
                txtMaPhieu.Text = qt.MaPhieuMuaHang;
                txtMaPhieu.ReadOnly = true;
                txtContent.Text = qt.PhieuMuaHang.NoiDung;
                cbbPhieuDeNghi.DataSource = null;
                cbbPhieuDeNghi.Items.Add(qt.PhieuDeNghiMuaHang);
                cbbPhieuDeNghi.SelectedItem = qt.PhieuDeNghiMuaHang;
                cbbPhieuDeNghi.Enabled = false;
            }
            else
            // fill combobox phieu de nghi
            {
                var listPhieuDeNghi = from qt in dc.QuyTrinhMuaHangs
                                      where qt.deleted == false &&
                                            qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_DE_NGHI
                                      select qt.PhieuDeNghiMuaHang;
                cbbPhieuDeNghi.DataSource = listPhieuDeNghi;

                txtMaPhieu.Text = _DB.DinhDangMaPhieu.getMaPhieuMoi(_DB.DinhDangMaPhieu.DINH_DANG_PHIEU_MUA_HANG_ID);

            }
        }

        private void cbbPhieuDeNghi_SelectedIndexChanged(object sender, EventArgs e)
        {
            _DB.PhieuDeNghiMuaHang p = (_DB.PhieuDeNghiMuaHang)cbbPhieuDeNghi.SelectedItem;
            if (p == null)
                return;

            // fill grid hang hoa
            dgvHangHoa.Rows.Clear();
            int count = 1;
            foreach (_DB.PhieuDeNghiMuaHang_HangHoa h in p.PhieuDeNghiMuaHang_HangHoas)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = h.HangHoa.TenHangHoa;
                r.Cells[2].Value = (h.SoLuongMua == null) ? h.SoLuongDeNghi.ToString() : h.SoLuongMua.ToString();
                r.Cells[3].Value = h.HangHoa.DonVi;
                if (h.HangHoa_NhaCungCap_ID == null)
                {
                    r.Cells[4].Value = h.NhaCungCapThamKhao;
                    r.Cells[5].Value = h.GiaThamKhao;
                }
                else
                {
                    _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                    _DB.HangHoa_NhaCungCap hn = dc.HangHoa_NhaCungCaps.Single(x => x.HangHoa_NhaCungCap_ID == h.HangHoa_NhaCungCap_ID);
                    r.Cells[4].Value = hn.NhaCungCap.TenNhaCungCap;
                    r.Cells[5].Value = hn.Gia;
                }

                r.Tag = h;

                dgvHangHoa.Rows.Add(r);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                // check chon ncc
                foreach (DataGridViewRow r in dgvHangHoa.Rows)
                {
                    _DB.PhieuDeNghiMuaHang_HangHoa h = (_DB.PhieuDeNghiMuaHang_HangHoa)r.Tag;
                    if (h.HangHoa_NhaCungCap_ID == null)
                    {
                        MessageBox.Show("Hãy chọn nhà cung cấp cho " + r.Cells[1].Value);
                        return;
                    }
                }

                // add new
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                if (_maPhieu == null)
                {
                    // new
                    _DB.PhieuMuaHang phieuMH = new _DB.PhieuMuaHang();
                    phieuMH.MaPhieu = txtMaPhieu.Text;
                    phieuMH.NoiDung = txtContent.Text;
                    phieuMH.MaNguoiTao = Program.MaNguoiDungHienTai;
                    phieuMH.NgayTao = DateTime.Now;
                    dc.PhieuMuaHangs.InsertOnSubmit(phieuMH);

                    // update
                    _DB.PhieuDeNghiMuaHang phieuDeNghi = (_DB.PhieuDeNghiMuaHang)cbbPhieuDeNghi.SelectedItem;
                    _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuDeNghiMuaHang == phieuDeNghi.MaPhieu);
                    qt.MaPhieuMuaHang = phieuMH.MaPhieu;
                    qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_TAO_PHIEU_MUA_HANG;
                }
                // edit
                else
                {
                    _DB.PhieuMuaHang phieuMH = dc.PhieuMuaHangs.Single(x => x.MaPhieu == _maPhieu);
                    phieuMH.NoiDung = txtContent.Text;
                }

                // update soluong mua
                foreach (DataGridViewRow r in dgvHangHoa.Rows)
                {
                    _DB.PhieuDeNghiMuaHang_HangHoa h = (_DB.PhieuDeNghiMuaHang_HangHoa)r.Tag;
                    _DB.PhieuDeNghiMuaHang_HangHoa hh = dc.PhieuDeNghiMuaHang_HangHoas.Single(x => x.ID == h.ID);
                    hh.HangHoa_NhaCungCap_ID = h.HangHoa_NhaCungCap_ID;
                    hh.SoLuongMua = float.Parse(r.Cells[2].Value as string);
                }

                // ok
                dc.SubmitChanges();
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void dgvHangHoa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // check
            if (e.RowIndex < 0 || e.ColumnIndex != 4)
                return;

            // choose
            _DB.PhieuDeNghiMuaHang_HangHoa h = (_DB.PhieuDeNghiMuaHang_HangHoa)dgvHangHoa.Rows[e.RowIndex].Tag;
            chonHangHoaNhaCCForm f = new chonHangHoaNhaCCForm(h.MaHangHoa);
            f.ShowDialog();

            // update
            if (f.getHangHoaNCCID() >= 0)
            {
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.HangHoa_NhaCungCap hn = dc.HangHoa_NhaCungCaps.Single(x => x.HangHoa_NhaCungCap_ID == f.getHangHoaNCCID());

                dgvHangHoa.Rows[e.RowIndex].Cells[4].Value = hn.NhaCungCap.TenNhaCungCap;
                dgvHangHoa.Rows[e.RowIndex].Cells[5].Value = hn.Gia;
                h.HangHoa_NhaCungCap_ID = hn.HangHoa_NhaCungCap_ID;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _DB.PhieuDeNghiMuaHang p = (_DB.PhieuDeNghiMuaHang)cbbPhieuDeNghi.SelectedItem;
            if (p != null)
            {
                XemPhieuDeNghiMuaHangForm f = new XemPhieuDeNghiMuaHangForm(p.MaPhieu);
                f.Show();
            }
        }


    }
}
